<?php
/**
 *
 * Hiearchy Selection Tree
 * @author Xavier VILAIN
 */
use Fox\SalesDistribution\Sales\Inquiry\Messages;
use Fox\MaterialManagement\Material;
use Fox\MaterialManagement\Material\Hierarchy;
$block = new Fox\Html\Block("bHiearchy",Msg(Material::BUNLDE_NAME,19));
$tree = new Fox\Html\Tree("hierarchyTree");
$tree->label(Msg(Messages::BUNDLE,Messages::ACTIVITIES));
$tree->fieldcat(array("id","parentid","code","label","level"));
$tree->setFieldAttr(array("id","parentid","code","level"), "SetHidden",true);
$tree->setFieldAttr(array("id","parentid","code","label","level"), "SetHiddenInput",true);
$tree->setCoche(true);
$tree->setExpandLevel(1);

if(isset($params['initialize']))
{
	$root = new Fox\Node(Fox\Node::ROOT_NODE);
	$root->key("id");
	$hiearchies = Hierarchy::loadTree();
	foreach($hiearchies as $hierarchy)
	{
		$parentid = $hierarchy['parentid'];
		if(!empty($parentid) && !isset($hiearchyTree[$parentid]))
			continue;
		if(empty($parentid))
			$hiearchyTree[$hierarchy['id']] = $root->add(new Fox\Node($hierarchy));
		else
			$hiearchyTree[$hierarchy['id']] = $hiearchyTree[$parentid]->add(new Fox\Node($hierarchy));
	}
	$tree->setExpandLevel(-1);
	$tree->initialize($root,true);
}

if(isset($params['tree']))
{
	return $tree;
}
$block->add($tree);
return $block;